from app.indicator import *
from lib.conn import Conn
from lib.func import *


def inc_rate(cb, cs):
    return (cb - cs) / cs


def trade2():
    conn = Conn()
    stocks = conn.fetch_all('select ts_code,pe,pb,rs from td_stock_ak where pe>0 and pe<50 and rs>=80 order by ts_code')
    for stock in stocks:
        end = None
        ts_code = stock['ts_code']
        di = daily_idc(ts_code, '20230101', end)
        if len(di) < 200:
            continue
        last20 = di.tail(20)
        down_days = len(last20[last20.ma_long_change < 0])
        if down_days > 8:
            continue
        last200 = di.tail(200)
        max_close = last200['high'].max()
        min_close = last200['low'].min()
        ld = di.iloc[-1]
        if ld['close'] < ld['ma_short']:
            continue
        if ld['ma_short'] < ld['ma_middle']:
            continue
        if ld['ma_middle'] < ld['ma_long']:
            continue
        if (ld['close'] - min_close) / min_close > 0.9:
            continue
        if (ld['close'] - min_close) / min_close < 0.10:
            continue
        if (max_close - ld['close']) / ld['close'] > 0.10:
            continue
        # if ld['rsi'] < 50:
        #     continue
        # if ld['strong_short'] > 0:
        #     continue
        if ld['strong_long'] < 0:
            continue
        wi = week_idc(ts_code, end)
        lw = wi.iloc[-1]
        if lw['macd_change'] < 0:
            continue
        if lw['ema_change'] < 0:
            continue
        # if lw['macd'] > 0:
        #     continue
        zf = str(round((ld['close'] - min_close) / min_close * 100, 2)) + '%'
        print(ld['ts_code'], stock['pe'], stock['pb'], stock['rs'], zf)





if __name__ == '__main__':
    trade2()



